﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Torch_Services
{
    public class BookApplyBookInfoService : IBookApplyBookInfoService
    {
        private IBookApplyBookInfoDal _bookApply;
        private IMapper _mapper;
        public BookApplyBookInfoService(IBookApplyBookInfoDal bookApply, IMapper mapper)
        {
            _bookApply = bookApply;
            _mapper = mapper;
        }

        public async Task<int> AddBookApplyBookInfoAsync(string title, string info, string publishingHouse, string photo, decimal price, int buyNumber, int state, int cateGory)
        {
            return await _bookApply.AddAsync(new Bookapplybookinfo 
            {
                Title = title,
                Info = info,
                PublishingHouse = publishingHouse,
                Photo = photo,
                Price = price,
                BuyNumber = buyNumber,
                State = state,
                Category = cateGory
            });
        }

        public async  Task<int> DeleteBookApplyBookInfoAsync(int id)
        {
            var data = await _bookApply.QueryAsync(id);
            return data == null ? -1 : await _bookApply.DeleteAsync(data);
        }

        public async  Task<int> EditBookApplyBookInfoAsync(int id, string title, string info, string publishingHouse, string photo, decimal price, int buyNumber, int state, int cateGory)
        {
            var data = await _bookApply.QueryAsync(id);
            if (data == null)
                return -1;
            data.Title = title;
            data.Info = info;
            data.PublishingHouse = data.PublishingHouse;
            data.Photo = photo;
            data.Price = price;
            data.BuyNumber = buyNumber;
            data.State = state;
            data.Category = cateGory;
            return await _bookApply.EditAsync(data);
        }

        public async Task<List<BookApplyBookInfoDto>> QueryBookApplyBookInfoAllAsync()
        {
            var data = await _bookApply.QueryAsync();
            return _mapper.Map<List<Bookapplybookinfo>,List<BookApplyBookInfoDto>>(data);
        }

        public async  Task<BookApplyBookInfoDto> QueryBookApplyBookInfoByIdAsync(int id)
        {
            var data = await _bookApply.QueryAsync(id);
            return _mapper.Map<Bookapplybookinfo, BookApplyBookInfoDto>(data);
        }

        public async  Task<List<BookApplyBookInfoDto>> QueryBookApplyBookInfoByNameAsync(string title)
        {
            var data = await _bookApply.QueryAsync(bf => bf.Title.Contains(title));
            return _mapper.Map<List<Bookapplybookinfo>, List<BookApplyBookInfoDto>>(data);
        }
    }
}
